package com.yunquan.ohana.ui.history.preview

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseActivity
import com.yunquan.ohana.databinding.ActivityHistoryPreviewBinding
import com.yunquan.ohana.entity.LocalFileEntity
import com.yunquan.ohana.ui.history.preview.adapter.HistoryPreviewAdapter
import com.yunquan.ohana.ui.photos.preview.adapter.PreviewAdapter
import com.yunquan.ohana.utils.ToastUtils

class HistoryPreviewActivity : BaseActivity<ActivityHistoryPreviewBinding, HistoryPreviewViewModel>() {

    private val dataList = mutableListOf<LocalFileEntity>()
    private var mAdapter: HistoryPreviewAdapter? = null

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_history_preview
    }

    override fun initVariableId(): Int {
        return BR.historyPreviewViewModel
    }

    override fun initParam() {
        super.initParam()
        window.statusBarColor = resources.getColor(R.color.black)
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE
    }

    @SuppressLint("SetTextI18n")
    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        GSYVideoType.setRenderType(GSYVideoType.GLSURFACE)
        GSYVideoType.enableMediaCodec()
        GSYVideoType.enableMediaCodecTexture()

        val bundle = intent.extras
        val index = bundle?.getInt("index") ?: 0
        val jsonData = bundle?.getString("data")
        if (jsonData != null) {
            val list = Gson().fromJson<List<LocalFileEntity>>(jsonData, object : TypeToken<List<LocalFileEntity>>() {}.type)
            dataList.addAll(list)
        } else {
            ToastUtils.showShort("未读取到数据")
        }
        binding!!.tvCount.text = "${index + 1}/${dataList.size}"
        mAdapter = HistoryPreviewAdapter(dataList)
        binding!!.vpContent.adapter = mAdapter
        binding!!.vpContent.offscreenPageLimit = 3
        binding!!.vpContent.setCurrentItem(index, false)
    }

    override fun initViewObservable() {
        super.initViewObservable()
        binding!!.vpContent.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            @SuppressLint("SetTextI18n")
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                val index = position % dataList.size
                binding!!.tvCount.text = "${index + 1}/${dataList.size}"
                val item = dataList[index]
                if (item.type == 2) {
                    val mChildView = binding!!.vpContent.getChildAt(0) as RecyclerView
                    val mHolder = mChildView.findViewHolderForAdapterPosition(position)
                    if (mHolder is PreviewAdapter.ViewHolder) {
                        if (mHolder.videoView.isInPlayingState) {
                            mHolder.videoView.post {
                                GSYVideoManager.instance().setDisplay(mHolder.videoView.currentSurface)
                            }
                        } else {
                            mHolder.videoView.post {
                                mHolder.videoView.startPlayLogic()
                            }
                        }
                        GSYVideoManager.onResume(false)
                    }
                } else {
                    GSYVideoManager.onPause()
                }
            }
        })
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev!!.action == MotionEvent.ACTION_DOWN) {
            binding!!.vpContent.requestDisallowInterceptTouchEvent(true)
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onResume() {
        super.onResume()
        GSYVideoManager.onResume(false)
    }

    override fun onPause() {
        super.onPause()
        GSYVideoManager.onPause()
    }

    override fun onDestroy() {
        GSYVideoManager.releaseAllVideos()
        super.onDestroy()
    }
}